local jianying = fk.CreateSkill {
  name = "rmt__jianying",
}

Fk:loadTranslationTable{
  ["rmt__jianying"] = "渐营",
  ["#rmt__jianying_trigger"] = "渐营",
  [":rmt__jianying"] = "当你使用牌时，若此牌与你使用的上一张牌点数或花色相同，你可以摸一张牌。"..
    "出牌阶段限一次，你可以将一张牌当任意一种基本牌使用，此牌花色视为你使用的上一张牌的花色。",

  ["#rmt__jianying-active"] = "渐营：将一张牌转化为任意基本牌使用",
  ["@rmt__jianying"] = "渐营",

  ["$rmt__jianying1"] = "良谋百出，渐定决战胜势！",
  ["$rmt__jianying2"] = "佳策数成，破敌垂手可得！",
}

local U = require "packages/utility/utility"

jianying:addEffect("viewas", {
  max_phase_use_time = 1,
  prompt = "#rmt__jianying-active",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    return U.CardNameBox {
      choices = player:getViewAsCardNames(jianying.name, all_names),
      all_choices = all_names,
      default_choice = "AskForCardsChosen",
    }
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk.all_card_types[self.interaction.data] ~= nil
  end,
  view_as = function(self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil or #cards ~= 1 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)

    local suitstrings = {"spade", "heart", "club", "diamond", "nosuit"}
    local suits = {Card.Spade, Card.Heart, Card.Club, Card.Diamond, Card.NoSuit}
    local colors = {Card.Black, Card.Red, Card.Black, Card.Red, Card.NoColor}
    local suit = player:getMark("rmt__jianying_suit")
    if table.contains(suitstrings, suit) then
      card.suit = suits[table.indexOf(suitstrings, suit)]
      card.color = colors[table.indexOf(suitstrings, suit)]
    end

    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:setPlayerMark(player, "rmt__jianying_used", 1)
  end,
  enabled_at_play = function(self, player)
    return player:getMark("rmt__jianying_used") == 0
  end,
})

jianying:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return data.from == player and (data.extra_data or {}).rmt__jianying_triggerable and player:hasSkill(jianying.name)
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, jianying.name)
  end,
})

jianying:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(jianying.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.card:getSuitString() == player:getMark("rmt__jianying_suit") or
        data.card.number == player:getMark("rmt__jianying_number") then
      data.extra_data = data.extra_data or {}
      data.extra_data.rmt__jianying_triggerable = true
    end
    room:setPlayerMark(player, "rmt__jianying_suit", data.card:getSuitString())
    room:setPlayerMark(player, "rmt__jianying_number", data.card.number)
    room:setPlayerMark(player, "@rmt__jianying", {data.card:getSuitString(true), data.card.number})
  end,
})

jianying:addAcquireEffect(function(self, player)
  local room = player.room
  local skill_event = room.logic:getCurrentEvent()
  local broke = true
  room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
    if e.id < skill_event.id then
      local use = e.data
      if use.from == player and broke then
        broke = false
        room:setPlayerMark(player, "rmt__jianying_suit", use.card:getSuitString())
        room:setPlayerMark(player, "rmt__jianying_number", use.card.number)
        room:setPlayerMark(player, "@rmt__jianying", {use.card:getSuitString(true), use.card.number})
      end
    end
  end, 0)
end)

jianying:addLoseEffect(function(self, player)
  local room = player.room
  room:setPlayerMark(player, "rmt__jianying_used", 0)
  room:setPlayerMark(player, "rmt__jianying_suit", 0)
  room:setPlayerMark(player, "rmt__jianying_number", 0)
  room:setPlayerMark(player, "@rmt__jianying", 0)
end)

return jianying
